package com.example.stringbufferlogger.Utils;

import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class Base64ToMultipartFileUtil implements MultipartFile {

    private final byte[] fileContent;
    private final String fileName;

    public Base64ToMultipartFileUtil(String base64Content, String fileName) {
        this.fileContent = Base64.getDecoder().decode(base64Content);
        this.fileName = fileName;
    }

    public static MultipartFile convert(String base64Content, String fileName) {
        return new Base64ToMultipartFileUtil(base64Content, fileName);
    }

    @Override
    public String getName() {
        return "file";
    }

    @Override
    public String getOriginalFilename() {
        return fileName;
    }

    @Override
    public String getContentType() {
        return "application/octet-stream";
    }

    @Override
    public boolean isEmpty() {
        return fileContent.length == 0;
    }

    @Override
    public long getSize() {
        return fileContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return fileContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(fileContent);
    }

    @Override
    public void transferTo(java.io.File dest) throws IOException, IllegalStateException {
        java.nio.file.Files.write(dest.toPath(), fileContent);
    }
}